package Json2RDF.Surface2RDF.SurfaceWriter;

import Json2RDF.Json2Java.OntologyClasses.SphericalSurface;
import Json2RDF.Surface2RDF.IndexTable.Face2ResourceIndexTable;
import Json2RDF.Surface2RDF.IndexTable.FaceIndexTable;
import RDFManager.PropertySet;
import RDFManager.RDFHead;
import RDFManager.RDFReader;
import RDFManager.ResourceSet;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;

import java.util.List;
import java.util.stream.IntStream;

public class SphericalSurfaceWriter {
  public static void writeList(List<SphericalSurface> sphericalSurfaceList) {

    IntStream.range(0, sphericalSurfaceList.size()).forEach(i -> {
      write(sphericalSurfaceList.get(i), "sphericalSurface" + (i + 1));
      Face2ResourceIndexTable.faceResourceIdxTbl.computeIfAbsent(sphericalSurfaceList.get(i), v -> RDFReader.model.getResource(RDFHead.namespace + "sphericalSurface" + (i + 1)));
      FaceIndexTable.faceIdxTbl.computeIfAbsent(sphericalSurfaceList.get(i).getId(), v -> sphericalSurfaceList.get(i));
    });
  }


  private static void write(SphericalSurface sphericalSurface, String localName) {
    Resource inst = RDFReader.model.createResource(RDFHead.namespace + localName);
    RDFReader.model.add(inst, RDF.type, ResourceSet.Face);
    RDFReader.model.add(inst, RDF.type, ResourceSet.sphericalSurface);
    inst.addLiteral(PropertySet.hasID, sphericalSurface.getId());
    inst.addLiteral(PropertySet.hasRadius, sphericalSurface.getRadius());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasCentralPoint", sphericalSurface.getCentralPoint());
  }
}
